package cate.game.role.pay.trademall.forevercontract;

import cate.common.table.d.GDNotice;
import cate.common.table.d.GDOperation;
import cate.common.table.trademall.forevercontract.ForeverContractRow;
import cate.common.util.GameResult;
import cate.game.chat.ChatContentParam;
import cate.game.res.MixRes;
import cate.game.role.Role;
import cate.game.role.RoleDependent;
import cate.game.role.pay.trademall.forevercontract.msg.ForeverContractDataResp;
import com.fasterxml.jackson.annotation.JsonIgnore;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;

import java.util.ArrayList;
import java.util.List;

@NoteClass(value = "永久契约特权")
public class ForeverContract extends RoleDependent {
    @NoteField("是否激活")
    public boolean active;

    @NoteField(value = "今日免费奖励")
    public boolean freeGot;

    @NoteField(value = "今日购买奖励")
    public boolean buyGot;

    @Override
    public void initialize(Role role) {
        super.initialize(role);
    }

    public GameResult<Void> allowPay() {
        GameResult<Void> r = new GameResult<>();
        if (active) {
            return r.fail("不能重复购买");
        }
        return r.success();
    }

    @JsonIgnore
    public GameResult<Void> getReward(){
        GameResult<Void> r = new GameResult<>();
        if((!active && freeGot) || (active && freeGot && buyGot)){
            return r.fail("今日的奖励已领完");
        }
        ForeverContractRow row = role.getGame().table.tradeMall.foreverContract.getRow();
        if (row == null) {
            return r.fail("奖励不存在");
        }
        MixRes reward = new MixRes();
        if(!freeGot){
            freeGot = true;
            reward.addList(new MixRes(row.freeRewardStr));
        }
        if(active && !buyGot) {
            buyGot = true;
            reward.addList(new MixRes(row.rewardStr));
        }
        if(!reward.isEmpty()){
            reward.add(role, true);
            role.getGame().notice.dialogReward(role, reward);
            noticeUpdate();
        }
        return r;
    }

    public void onPaySuccess(int tid){
        ForeverContractRow row = role.getGame().table.tradeMall.foreverContract.get(tid);
        if (row != null) {
            active = true;
            MixRes buyReward = new MixRes(row.buyRewardStr);
            buyReward.add(role, true, GDOperation.FOREVER_CONTRACT_BUY_REWARD);
            role.getGame().notice.dialogReward(role, buyReward);
            noticeUpdate();
            lampOnline();
        }
    }

    private void lampOnline(){
    }

    @Override
    public void onDaySpan(boolean silence) {
        freeGot = false;
        buyGot = false;
        if(!silence){
            noticeUpdate();
        }
    }

    public void noticeUpdate(){
        role.sendNow(new ForeverContractDataResp(this));
    }
}
